package pl.krzosekm.vehicleregister.gui;
import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import pl.krzosekm.vehicleregister.model.Vehicle;
import pl.krzosekm.vehicleregister.service.Utils;
import pl.krzosekm.vehicleregister.service.VehicleService;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class VehiclePanel extends JPanel {
	
    private JLabel regNoLabel;
    private JButton repairRegisterButton;
	private JLabel yearOfManLabel;
	private JButton saveVehButton;
	private JLabel mileAgeHint;
	private JLabel yearOfManHint;
	private JLabel modelHint;
	private JLabel brandHint;
	private JLabel vinHint;
	private JLabel regNoHint;
	private JTextField mileageTF;
	private JTextField yearOfManTF;
	private JTextField modelTF;
	private JTextField brandTF;
	private JTextField regNoTF;
	private JTextField vinTF;
	private JLabel mileAgeLabel;
	private JLabel modelLabel;
	private JLabel brandLabel;
	private JLabel vinLabel;
	private JButton newVehButton;
	private JButton jButton;
	
	private SimpleDateFormat yearOfManFormat = new SimpleDateFormat("yyyy");
	
	private String[] locations = {"classpath:pl/krzosekm/vehicleregister/resources/applicationContext-service.xml"};
	private ApplicationContext ctx = new FileSystemXmlApplicationContext(locations);
		
	@Autowired
	private VehicleService vehicleService;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new VehiclePanel(null));
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public VehiclePanel(JButton jButton) {
		super();
		this.jButton = jButton;
		vehicleService = (VehicleService)ctx.getBean("vehicleService");
		initGUI();
	}
	
	private void initGUI() {
		try {
		    GroupLayout thisLayout = new GroupLayout((JComponent)this);
		    this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(544, 272));
			{
			    newVehButton = new JButton();
			    newVehButton.setName("newVehButton");
			    newVehButton.setAction(getAppActionMap().get("newVehicleAction"));
			}
			{
			    vinLabel = new JLabel();
			    vinLabel.setName("vinLabel");
			}
			{
				repairRegisterButton = jButton;
				repairRegisterButton.setName("repairRegisterButton");
			}
			{
			    saveVehButton = new JButton();
			    saveVehButton.setName("saveVehButton");
			    saveVehButton.setAction(getAppActionMap().get("saveAction"));
			}
			{
			    vinHint = new JLabel();
			}
			{
			    brandHint = new JLabel();
			}
			{
			    modelHint = new JLabel();
			}
			{
			    yearOfManHint = new JLabel();
			}
			{
			    mileAgeHint = new JLabel();
			}
			{
			    regNoHint = new JLabel();
			}
			{
			    regNoLabel = new JLabel();
			    regNoLabel.setName("regNoLabel");
			}
			{
			    brandLabel = new JLabel();
			    brandLabel.setName("brandLabel");
			}
			{
			    modelLabel = new JLabel();
			    modelLabel.setName("modelLabel");
			}
			{
			    mileAgeLabel = new JLabel();
			    mileAgeLabel.setName("mileAgeLabel");
			}
			{
			    vinTF = new JTextField();
			    vinTF.setName("vinNumber");
			    vinTF.addFocusListener(new FocusAdapter() {
				public void focusLost(FocusEvent evt) {
				    vinFocusLost(evt);
				}
			    });
			}
			{
			    regNoTF = new JTextField();
			    regNoTF.setName("regNo");
			    regNoTF.addFocusListener(new FocusAdapter() {
				public void focusLost(FocusEvent evt) {
				    if(regNoTF.getText() != null && regNoTF.getText().length() > 0)
				    {
					regNoTF.setText(Utils.toUpper(regNoTF.getText()));
				    }
				}
			    });
			}
			{
			    brandTF = new JTextField();
			    brandTF.setName("brand");
			    brandTF.addFocusListener(new FocusAdapter() {
				public void focusLost(FocusEvent evt) {
				    if(brandTF.getText() != null && brandTF.getText().length() > 0)
				    {
					brandTF.setText(Utils.firstCharToUpper(brandTF.getText()));
				    }
				}
			    });
			}
			{
			    modelTF = new JTextField();
			    modelTF.setName("model");
			    modelTF.addFocusListener(new FocusAdapter() {
				public void focusLost(FocusEvent evt) {
				    if(modelTF.getText() != null && modelTF.getText().length() > 0)
				    {
					modelTF.setText(Utils.firstCharToUpper(modelTF.getText()));
				    }
				}
			    });
			}
			{
			    yearOfManTF = new JTextField();
			    yearOfManTF.setName("yearOfMan");
			}
			{
			    mileageTF = new JTextField();
			    mileageTF.setName("mileage");
			}
			{
			    yearOfManLabel = new JLabel();
			    yearOfManLabel.setName("yearOfManLabel");
			}
		    thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
		    	.addContainerGap()
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(vinHint, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
		    	        .addGap(6))
		    	    .addComponent(vinTF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(vinLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)))
		    	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    	        .addComponent(regNoLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	        .addComponent(regNoTF, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(regNoHint, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)
		    	        .addGap(8)))
		    	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(brandHint, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
		    	        .addGap(6))
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(brandLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
		    	    .addComponent(brandTF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(modelHint, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
		    	        .addGap(6))
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(modelLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
		    	    .addComponent(modelTF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(yearOfManHint, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
		    	        .addGap(6))
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(yearOfManLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
		    	    .addComponent(yearOfManTF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(mileAgeHint, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
		    	        .addGap(6))
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addComponent(mileAgeLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
		    	    .addComponent(mileageTF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	.addGap(36)
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    	        .addComponent(newVehButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	        .addComponent(saveVehButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		    	        .addComponent(repairRegisterButton, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
		    	        .addGap(0, 0, Short.MAX_VALUE)))
		    	.addContainerGap(33, 33));
		    thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
		    	.addContainerGap()
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(thisLayout.createSequentialGroup()
		    	        .addGroup(thisLayout.createParallelGroup()
		    	            .addComponent(mileAgeLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
		    	            .addComponent(yearOfManLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
		    	            .addComponent(modelLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
		    	            .addComponent(brandLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
		    	            .addComponent(regNoLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
		    	            .addComponent(vinLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
		    	        .addGroup(thisLayout.createParallelGroup()
		    	            .addGroup(thisLayout.createSequentialGroup()
		    	                .addGroup(thisLayout.createParallelGroup()
		    	                    .addComponent(mileageTF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
		    	                    .addComponent(yearOfManTF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
		    	                    .addComponent(regNoTF, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE))
		    	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		    	                .addGroup(thisLayout.createParallelGroup()
		    	                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                        .addComponent(mileAgeHint, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
		    	                        .addGap(0, 34, Short.MAX_VALUE))
		    	                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                        .addComponent(yearOfManHint, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
		    	                        .addGap(0, 34, Short.MAX_VALUE))
		    	                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                        .addComponent(regNoHint, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
		    	                        .addGroup(thisLayout.createParallelGroup()
		    	                            .addGroup(thisLayout.createSequentialGroup()
		    	                                .addComponent(modelHint, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
		    	                            .addGroup(thisLayout.createSequentialGroup()
		    	                                .addComponent(brandHint, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
		    	                            .addGroup(thisLayout.createSequentialGroup()
		    	                                .addComponent(vinHint, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)))
		    	                        .addGap(0, 0, Short.MAX_VALUE))))
		    	            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                .addComponent(modelTF, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(0, 47, Short.MAX_VALUE))
		    	            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                .addComponent(brandTF, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(0, 47, Short.MAX_VALUE))
		    	            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                .addComponent(vinTF, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(0, 47, Short.MAX_VALUE)))
		    	        .addGap(7))
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addGap(0, 78, Short.MAX_VALUE)
		    	        .addComponent(repairRegisterButton, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)))
		    	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 1, GroupLayout.PREFERRED_SIZE)
		    	.addComponent(newVehButton, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
		    	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
		    	.addComponent(saveVehButton, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
		    	.addContainerGap(44, 44));
		    thisLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {vinLabel, regNoLabel, brandLabel, modelLabel, yearOfManLabel, mileAgeLabel});
			newVehicleAction();
		    Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		

	}
	
	public JTextField getVin() {
	    return vinTF;
	}
	
	public JTextField getRegNo() {
	    return regNoTF;
	}
	
	public JTextField getBrand() {
	    return brandTF;
	}
	
	public JTextField getModel() {
	    return modelTF;
	}
	
	public JTextField getJTextField1() {
	    return yearOfManTF;
	}
	
	public JTextField getMileage() {
	    return mileageTF;
	}
	
	public void setVehicleService(VehicleService vehicleService) {
	    this.vehicleService = vehicleService;
	}

	public VehicleService getVehicleService() {
	    return vehicleService;
	}


	/** ACTIONS **/
	
	private ApplicationActionMap getAppActionMap() {
		return Application.getInstance().getContext().getActionMap(this);
	}
	
	@Action
	public void editAction() {
	    vinTF.setEditable(false);
	    regNoTF.setEditable(true);
	    brandTF.setEditable(true);
	    modelTF.setEditable(true);
	    yearOfManTF.setEditable(true);
	    mileageTF.setEditable(true);
	    
	    saveVehButton.setText("Zapisz");
	    saveVehButton.setAction(getAppActionMap().get("saveAction"));
	}
	
	@Action
	public void saveAction() {
	    
	    String vin = vinTF.getText();
	    String registerNo = regNoTF.getText();
	    String brand = brandTF.getText();
	    String model = modelTF.getText();
	    Date yearOfMan = null;
	    
	    try {
	    	yearOfMan = yearOfManFormat.parse(yearOfManTF.getText());
	    } catch (ParseException e) {
	    	e.printStackTrace();
	    }
	    
	    Integer mileage = Integer.parseInt(mileageTF.getText());
	    
	    Vehicle newVehicle = new Vehicle(vin, registerNo, brand, model, yearOfMan, mileage);
	    vehicleService.save(newVehicle);
	    
	    vinTF.setEditable(true);
	    regNoTF.setEditable(false);
	    mileageTF.setEditable(false);
	    modelTF.setEditable(false);
	    yearOfManTF.setEditable(false);
	    brandTF.setEditable(false);
	    
	    saveVehButton.setText("Zmień");
	    saveVehButton.setAction(getAppActionMap().get("editAction"));
	    
	}

	private void vinFocusLost(FocusEvent evt) {
	    String vin = vinTF.getText();
	    
	    if(vin != null && vin.length() > 0)
	    {
			vin = Utils.toUpper(vin);
			vinTF.setText(vin);
			vinTF.setEditable(false);
			
			Vehicle vehicle = vehicleService.getVehicleByVin(vin);
			
			if(vehicle != null)
			{
			    regNoTF.setText(vehicle.getRegisterNo());
			    brandTF.setText(vehicle.getBrand());
			    modelTF.setText(vehicle.getModel());
			    yearOfManTF.setText(yearOfManFormat.format(vehicle.getYearOfMan()));
			    mileageTF.setText(String.valueOf(vehicle.getMileage()));
			    
			    saveVehButton.setText("Zmień");
			    saveVehButton.setAction(getAppActionMap().get("editAction"));
			}
			else
			{
			    regNoTF.setEditable(true);
			    brandTF.setEditable(true);
			    mileageTF.setEditable(true);
			    modelTF.setEditable(true);
			    yearOfManTF.setEditable(true);
			}
	    }
	}
	
	@Action
	public void newVehicleAction() {
	    vinTF.setText(null);
	    regNoTF.setText(null);
	    mileageTF.setText(null);
	    modelTF.setText(null);
	    yearOfManTF.setText(null);
	    brandTF.setText(null);
	    
	    vinTF.setEditable(true);
	    regNoTF.setEditable(false);
	    mileageTF.setEditable(false);
	    modelTF.setEditable(false);
	    yearOfManTF.setEditable(false);
	    brandTF.setEditable(false);
	}

	
	
	

}
